package bolsa;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class operadorIndex {
	String emailUser="";
	String contraUser="";
	String nomEmp="";
	private String resultado="";
	private Boolean bandera = false;
	private Boolean bandEmpresa = false;
	private Boolean bandUser = false;
	private String indexUrl="";
	
	public String ofertaIndex(){
		try{
			Class.forName("com.mysql.jdbc.Driver");
			  Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bolsita","root","");
			  Statement stmt = con.createStatement();			  
			  String sql = "SELECT * FROM oferta ORDER BY id DESC";
			  ResultSet result = stmt.executeQuery(sql);
		      while (result.next()){
		    	  resultado+="<li><a href='ofertaDetail.jsp?id="+result.getString("id")+"&bandera="+getBandUser()+"&user="+getEmailUser()+"'><h2>"+result.getString("fecha")+"</h2><p>"+result.getString("puesto")+"</p><p>"+result.getString("provincia")+"</p><p>"+result.getString("nombreEmp")+"</p></a></li>";		    	      
		      }
		      con.close();
		}catch(Exception ex){
			System.out.println(ex.getMessage());
		}
		return resultado;	
	}
	
	public Boolean getOk(){
		try {
	  		String sql = "SELECT email,contra,nomEmp FROM empresa WHERE email =  '"+ getEmailUser() + "' and contra = '" + getContraUser() + "'";
	  		Class.forName("com.mysql.jdbc.Driver");
	  		Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bolsita", "root", "");
	  		Statement stmt = con.createStatement();
	  		ResultSet result = stmt.executeQuery(sql);
	  		while (result.next()) {
	  			setNomEmp(result.getString("nomEmp"));
	  			bandera = true;
	  			setBandEmpresa(true);
	  		}
	  		con.close();
	  		if(!bandera){
	  			String sql1 = "SELECT email,contra FROM candidato WHERE email =  '"+ getEmailUser() + "' and contra = '" + getContraUser() + "'";
		  		Class.forName("com.mysql.jdbc.Driver");
		  		Connection con1 = DriverManager.getConnection("jdbc:mysql://localhost:3306/bolsita", "root", "");
		  		Statement stmt1 = con1.createStatement();
		  		ResultSet result1 = stmt1.executeQuery(sql1);
		  		while (result1.next()) {
		  			bandera = true;
		  			setBandUser(true);
		  		}
		  		con1.close();
	  		}	  		
	  		
	  	} catch (Exception ex) {
	  		// ups!
	  		ex.printStackTrace();
	  		//out.println(ex.getMessage());
	  	}
		return bandera;
	}
	
	public String getIndexUrl(String tipo){
		if(tipo=="empresa"){
			setIndexUrl("ofertas.jsp?nomEmp="+getNomEmp());
		}else{
			setIndexUrl("ofrecimientos.jsp");
		}
		return indexUrl;
	}
	
	public String getEmailUser() {
		return emailUser;
	}

	public void setEmailUser(String emailUser) {
		this.emailUser = emailUser;
	}

	public String getContraUser() {
		return contraUser;
	}
	public void setContraUser(String contraUser) {
		this.contraUser = contraUser;
	}

	public Boolean getBandera() {
		return bandera;
	}

	public void setBandera(Boolean bandera) {
		this.bandera = bandera;
	}

	public void setIndexUrl(String indexUrl) {
		this.indexUrl = indexUrl;
	}

	public String getNomEmp() {
		return nomEmp;
	}

	public void setNomEmp(String nomEmp) {
		this.nomEmp = nomEmp;
	}

	public Boolean getBandEmpresa() {
		return bandEmpresa;
	}

	public void setBandEmpresa(Boolean bandEmpresa) {
		this.bandEmpresa = bandEmpresa;
	}

	public Boolean getBandUser() {
		return bandUser;
	}

	public void setBandUser(Boolean bandUser) {
		this.bandUser = bandUser;
	}
	
}
